//
//  FengButton.m
//  A Learning Walk
//
//  Created by dllo on 16/6/15.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "FengButton.h"

#define kTitleRatio 0.2

@interface FengButton ()

@property (copy, nonatomic) tapHandler handler;

@end

@implementation FengButton

-(void)dealloc{
    
    Block_release(_handler);
    [super dealloc];
}

+(id)buttonWithType:(UIButtonType)buttonType frame:(CGRect)frame title:(NSString *)title image:(UIImage *)image color:(UIColor *)color handler:(tapHandler)handler{
    
    FengButton *button = [super buttonWithType:buttonType];
    button.frame = frame;
    //    button.titleLabel.textAlignment = NSTextAlignmentCenter;//文字居中
    //    button.titleLabel.font = [UIFont systemFontOfSize:18];//文字大小,可以改
    button.backgroundColor = color;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.imageView.contentMode = UIViewContentModeCenter;//图片的内容模式
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    button.handler = handler;
    return button;
    
}

+(void)btnTapped:(FengButton *)sender{
    if (sender.handler) {
        sender.handler(sender);
    }
    
}
#pragma mark -- 调整内部ImageView的frame
-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageWidth = contentRect.size.width;
    CGFloat imageHeight = contentRect.size.height*(1-kTitleRatio);
    return CGRectMake(imageX, imageY, imageWidth, imageHeight);
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleX = 0;
    CGFloat titleHeight = contentRect.size.height*kTitleRatio;
    CGFloat titleY = contentRect.size.height-titleHeight-3;
    CGFloat titleWidth = contentRect.size.width;
    return CGRectMake(titleX, titleY, titleWidth, titleHeight);
}

@end
